//获取应用实例


const app = getApp()

Page({
  data: {
    uid:'',
    phone:'',
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    count: 1,
    imagePath: "../utils/Test.JPG",
    position: 'front',
    src: ''
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },

  userNameInput: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },

  //鼠标触发的时候的方法----否则取不到phone的值
  inputchange:function(e){
    const phonevalue = e.detail.value;
    this.setData({
      phone: phonevalue
    })
    console.log("shouji",this.data.phone)
  },
  onLoad: function () {
    this.ctx = wx.createCameraContext()
    this.data.phone = wx.getStorageSync('phone');
    this.setData({
      phone: this.data.phone
    })
    this.setData({

      firHorse: true,
      secHorse:false,
      thrHorse:false,
      forHorse:false


    });
    // var APIKEY = authData["APIKEY"];
    // var timestamp = authData["timestamp"];
    // var nonce= authData["nonce"];
    // var signature= authData["signature"];
    // var APISECRET = authData["api_secret"];

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }, clickMe: function () {

    this.setData({ msg: "第一个成型" })
  },

  add: function (e) {
    this.setData({
      count: this.data.count + 1
    })
  },
  tapName: function (event) {
    console.log(event)
  },

  takePhoto() {
    this.ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        console.log('=====res======:', res.tempImagePath)

        this.setData({
          src: res.tempImagePath

          // this.selectImage(src)
        })
      }
    })
  },
  startRecord() {
    wx.showLoading({
      title: '录制中',
    }),
    this.ctx.startRecord({
      
      success: () => {
        console.log('startRecord')
      }
    })
  },
  stopRecord() {
    this.ctx.stopRecord({

      success: (res) => {

        this.setData({
          src: res.tempThumbPath,
          videoSrc: res.tempVideoPath
        })

        // wx.showLoading({
        //   title: '视频上传中'
        // });
        var phone = this.data.phone
        console.log("shouji",this.data.phone);
        wx.uploadFile({
          // url: "http://119.3.4.50:8180/safe/mini/uploadVedioFile", // 你的接口地址
          url: "https://aqhy.citydo.com.cn/safe/mini/videoRecognition",
          filePath: res.tempVideoPath,
          name: "file",
          method: 'POST',
          header: {
            'content-type': 'multipart/form-data',
          },
          formData: {
            phone: phone
          },
          success: function (res) {
            var data = res.data
            res = JSON.parse(res.data)
            wx.hideLoading();

            if (res.code == 0) {
              console.info(res.msg)

              if (res.data == phone) {

                wx.showModal({
                  title: '提示',
                  content: '验证通过',

                  success: function(res) {
                    wx.redirectTo({
                      url: '../index/index'
                    })
                  }

                })
              }else {
                wx.showModal({
                  title: '提示',
                  content: '不是本人',
                  success: function (res) {
                    wx.redirectTo({
                      url: '../index/index'
                    })
                  }
                })
              }

            } else {
              wx.showModal({
                title: '提示',
                content: res.msg
              })
            }
          },
          fail: function () {
            wx.showModal({
              title: '提示',
              content: '网络失败',
              showCancel: false,
              success: function (res) {

                wx.redirectTo({
                  url: '../index/index'
                })

              }
            })
          },
          complete: () => {


          }

        })

      }
    })
  },
  error(e) {
    console.log(e.detail)
  }

})
